package com.example.smarttrafficgitee.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.smarttrafficgitee.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class P03RechargeLogFragment extends Fragment {

    private List<BillRecords> billRecords;
    private Spinner spMainChoice;
    private Button btnMainInquire;
    private RecyclerView ryMainTable;
    private TextView tvMainTip;

    public P03RechargeLogFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_p03_recharge_log, container, false);
        initView(view);
        Data();
        tvMainTip.setVisibility(View.GONE);
        if(null == billRecords || billRecords.size() ==0 ){//如果无历史记录则运行
            tvMainTip.setVisibility(View.VISIBLE);
            tvMainTip.setText("暂无历史记录");
        }
        ryMainTable.setVisibility(View.GONE);//先隐藏表格

        spMainChoice.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String Name = parent.getItemAtPosition(position).toString();//提取选项的文本
                switch (Name){
                    case "时间升序":
                        Collections.sort(billRecords, new Comparator<BillRecords>() {
                            @Override
                            public int compare(BillRecords o1, BillRecords o2) {
                                int diff = o1.getSerialnumber() - o2.getSerialnumber();
                                if (diff > 0) {
                                    return 1;
                                }else if (diff < 0) {
                                    return -1;
                                }
                                return 0;
                            }
                        });
                        break;
                    case "时间降序":
                        Collections.sort(billRecords, new Comparator<BillRecords>() {
                            @Override
                            public int compare(BillRecords o1, BillRecords o2) {
                                int diff = o1.getSerialnumber() - o2.getSerialnumber();
                                if (diff > 0) {
                                    return -1;
                                }else if (diff < 0) {
                                    return 1;
                                }
                                return 0;
                            }
                        });
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        btnMainInquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayoutManager layout = new LinearLayoutManager(container.getContext(), RecyclerView.VERTICAL, false);
                ryMainTable.setLayoutManager(layout);
                BillRecords_Adapter adapter = new BillRecords_Adapter(billRecords);
                ryMainTable.setAdapter(adapter);
                ryMainTable.setVisibility(View.VISIBLE);
            }
        });
        return view;
    }

    private void Data() {
        billRecords = new ArrayList<>();
        billRecords.add(new BillRecords(1, 1, 100, "admin", "2017.3.18 13.23"));
        billRecords.add(new BillRecords(2, 1, 8, "admin", "2017.3.17 13.23"));
        billRecords.add(new BillRecords(3, 2, 80, "admin", "2017.3.16 13.23"));
    }
    private void initView(View View) {
        spMainChoice = (Spinner) View.findViewById(R.id.spMainChoice);
        btnMainInquire = (Button) View.findViewById(R.id.btnMainInquire);
        ryMainTable = (RecyclerView) View.findViewById(R.id.ryMainTable);
        tvMainTip = (TextView)View.findViewById(R.id.tvMainTip);
    }
}
